const db = require('db/dictionary.js');
const dbPackage = require('db/package.js');

/** @name 获取特定类型的字段列表 **/
exports.singleList = async ctx => {
  if(ctx.requiredParams(['type'])) return;
	const { type } = ctx.query;
	const options = { type };
  const rows = await db.yd_singleList(options);
	ctx.success({
    total: rows.length,
    rows
  });
};

/** @name 获取支付方式，由字典和套餐组成 **/
exports.getPayType = async ctx => {
	const rows = await exports.getPayTypeFun()
	ctx.success({
    total: rows.length,
    rows
  });
};

/** @name 获取支付方式，由字典和套餐组成 **/
exports.getPayTypeFun = () => {
	return new Promise(async resolve => {
		let dictionaryRes = await db.yd_singleList({ type: 'pay_type' });
		dictionaryRes = dictionaryRes.map(item => ({ label: item.label, value: item.value }))
		let packageRes = await dbPackage.yd_list();
		packageRes = packageRes.map(item => {
			if (item.type === 0) {
				return {
					label: '自由车位-' + item.name,
					value: item.code
				}
			} else {
				return {
					label: '固定车位-' + item.name,
					value: item.code
				}
			}
		})
		const rows = [...dictionaryRes, ...packageRes]
		resolve(rows)
	})
}
